﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GorgonLibrary.Graphics;
using EngineOfEvermore.Editor.Common;
using System.Diagnostics;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Imaging;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.Editor.AnimationEditor.View
{
    internal partial class DocumentView : WeifenLuo.WinFormsUI.Docking.DockContent, IDocumentView
    {
        #region Variables
        EdibleFrameAnimation _displayedAnimation;
        AnimatedSprite _sprite;
        SceneObject _previewSceneObject;
        MinimalSceneGraphContext _previewSceneContext;
        SceneGraph _previewScene;
        String _documentTitle = "";
        IInputListener _inputListener;
        #endregion

        public EdibleFrameAnimation DisplayedAnimation
        {
            get
            {
                return _displayedAnimation;
            }
            set
            {
                if ( _displayedAnimation == value )
                    return;

                VisualComponent vc = _previewSceneObject.GetComponent<VisualComponent>();

                if ( _displayedAnimation != null )
                {
                    vc.Children.Remove( _sprite );
                    _sprite = null;
                }

                _displayedAnimation = value;

                if ( _displayedAnimation != null )
                {
                    _sprite = new AnimatedSprite();
                    _sprite.Animations["Main"] = 
                        FrameAnimation.CreateEdibleAnimationWrapper( value );
                    vc.Children.Add( _sprite );

                    _sprite.CurrentAnimation = "Main";
                }
            }
        }

        public String DocumentTitle
        {
            get { return _documentTitle; }
            set 
            {
                Debug.Assert( value != null );

                _documentTitle = value;
                this.Text = _documentTitle;
                this.TabText = _documentTitle;
            }
        }

        public DocumentView()
        {
            InitializeComponent();

            if ( !this.DesignMode )
                throw new Exception( "Use the other constructor!" );
        }
 
        public DocumentView( GorgonRenderTargetManager renderTargetManager, 
            IInputListener inputListener )
        {
            Debug.Assert( renderTargetManager != null );

            InitializeComponent();

            _inputListener = inputListener;

            _previewSceneView.Initialize( renderTargetManager );

            _previewSceneContext = new MinimalSceneGraphContext();
            _previewScene = new SceneGraph();

            _previewScene.OnInit( _previewSceneContext.ServiceProvider );

            _previewSceneObject = new SceneObject();
            _previewScene.AddSceneObject( _previewSceneObject );
            _previewSceneObject.AddComponent( new VisualComponent() );
            _previewSceneView.SceneGraph = _previewScene;

            _framesSceneView.Initialize( renderTargetManager );

            GorgonLibrary.Gorgon.Idle += new FrameEventHandler( _updateScenes );
            
            _previewSceneContext.UpdaterTargetFps = 
                GorgonLibrary.Gorgon.DesktopVideoMode.RefreshRate;
        }

        public void ResetPrieviewCamera()
        {
            _previewSceneView.Camera.Position = Vector2d.Zero;
            _previewSceneView.Camera.Scale = 1.0f;
        }

        void _updateScenes( object sender, FrameEventArgs e )
        {
            _previewSceneContext.UpdateTimeContext();
        }

        void _splitContainer_SplitterMoving( object sender, SplitterCancelEventArgs e )
        {
            GorgonLibrary.Gorgon.Stop();
        }

        void _splitContainer_SplitterMoved( object sender, SplitterEventArgs e )
        {
            GorgonLibrary.Gorgon.Go();
        }
    }
}
